Option Explicit On Option Strict On Public Class Form1 Dim numbers(100) As Double Dim compared(100) As Double ' compared to average Dim sorted(100) As Double Dim numItems As Integer Private Function ExtractLine(ByVal text As String, ByRef line As String, ByRef startPos As Integer, ByRef foundPos As Integer) As Boolean ' find the next occurrence of the new line char foundPos = text.IndexOf(ControlChars.NewLine, startPos) ' check if it was found If foundPos = -1 Then Return False Else ' pull out the current line of the file line = text.Substring(startPos, foundPos - startPos) ' update the starting point for search startPos = foundPos + 2 Return True End If End Function ' general function to calculate mean of any array of doubles Private Function CalcAverage(ByVal data() As Double, ByVal numOfItems As Integer) As Double Dim total As Double Dim ave As Double Dim cnt As Integer For cnt = 0 To numOfItems - 1 total = total + data(cnt) Next cnt ave = total / numItems Return ave End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim allText As String Dim lineExists As Boolean Dim current As String Dim start As Integer = 0 Dim foundPos As Integer ' check if file exists If My.Computer.FileSystem.FileExists("numbers.txt") Then ' read the file contents allText = My.Computer.FileSystem.ReadAllText("numbers.txt") ' while not at end, extract line Do lineExists = ExtractLine(allText, current, start, foundPos) If lineExists Then ' stick the line in the list box rtbData.AppendText(current & ControlChars.NewLine) Dim isConverted As Boolean isConverted = Double.TryParse(current, numbers(numItems)) If isConverted Then numItems = numItems + 1 End If End If Loop While lineExists Else MsgBox("Please put the numbers.txt file in the bin folder for the project") End If End Sub Private Sub btnHowMany_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHowMany.Click Dim threshold As Double Dim isConverted As Boolean Dim numAbove As Integer ' convert threshold to number isConverted = Double.TryParse(txtThreshold.Text, threshold) If isConverted Then ' loop through the array finding out how many are larger than the threshold Dim cnt As Integer For cnt = 0 To numItems - 1 ' just for fun 'MsgBox("Current number is: " & numbers(cnt).ToString) ' figure out if numberr is above threshold If numbers(cnt) > threshold Then ' is above threshold numAbove = numAbove + 1 End If Next cnt txtAnswer.Text = numAbove.ToString End If End Sub Private Sub btnAve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAve.Click Dim ave As Double ave = CalcAverage(numbers, numItems) TxtAve.Text = ave.ToString("n2") End Sub Private Sub btnMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMax.Click Dim max As Double = -9999999999 For cnt As Integer = 0 To numItems - 1 If numbers(cnt) > max Then max = numbers(cnt) End If Next cnt txtMax.Text = max.ToString End Sub ' display for each element in array its difference from the average Private Sub btnCompareToAve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompareToAve.Click Dim ave As Double ave = CalcAverage(numbers, numItems) ' illegal example ' compared = numbers - ave Dim cnt As Integer ' set compared to compare to average For cnt = 0 To numItems - 1 compared(cnt) = numbers(cnt) - ave Next cnt ' display comparisons in rich text box For cnt = 0 To numItems - 1 rtbCompared.AppendText(compared(cnt) & ControlChars.NewLine) Next cnt End Sub Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click Dim cnt As Integer sorted = numbers Array.Sort(sorted) Array.Reverse(sorted) ' display comparisons in rich text box For cnt = 0 To numItems - 1 rtbSorted.AppendText(sorted(cnt) & ControlChars.NewLine) Next cnt End Sub Private Sub btnSortAsc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSortAsc.Click rtbSorted.Clear() Dim cnt As Integer sorted = numbers Array.Sort(sorted) ' display comparisons in rich text box For cnt = 100 - (numItems - 1) To 100 rtbSorted.AppendText(sorted(cnt) & ControlChars.NewLine) Next cnt End Sub End Class